package com.yhm.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.yhm.dao.AllDAO;
import com.yhm.model.OrderFrom;
import com.yhm.model.Redo;
import com.yhm.model.Trace;
import com.yhm.service.yhmService;


@WebService
public class yhmServiceImpl implements yhmService{
	
	private SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private AllDAO allDAO;

	public AllDAO getAllDAO() {
		return allDAO;
	}

	@Resource(name = "allDAO")
	public void setAllDAO(AllDAO allDAO) {
		this.allDAO = allDAO;
	}
	
	/**
	 * whether qc1 0 ; qc2 1 ; 其他为 null
	 * status  出入状态，入1 , 出0，
	 * pnumber QCID
	 */
	@SuppressWarnings("unchecked")
	public String traceServlet(@WebParam(name = "companyNum")String companyNum, @WebParam(name = "department")String department,@WebParam(name = "whether")String whether,@WebParam(name = "status")String status,@WebParam(name = "pnumber")String pnumber) {
		OrderFrom order = new OrderFrom();
		order = (OrderFrom) allDAO.getOrderFromDAO().findById(companyNum);
		if(order==null){
			return companyNum+" 为无效单号！！*";
		}
		if(!(order.getQc2().equals("0"))){
			return companyNum+" 已经出货，请不要重复扫描！！*";
		}
		//System.out.println(pnumber);
		//System.out.println(status);
		//System.out.println(whether);
		if(pnumber!=null&&status.equals("0")&&whether.equals("1")){//QC1出
			order.setQc1(pnumber);
		}
		if(pnumber!=null&&status.equals("0")&&whether.equals("2")){//QC2出货
			order.setQc2(pnumber);
		}
		
		List<Trace> trace_list = (List<Trace>) allDAO.getTraceDAO().findhql("from Trace t where t.companyNum = '"+companyNum+"' order by t.regtime DESC");
		if(status.equals("1")){
			//第一个部门
			if(trace_list.size()==0||trace_list==null){
				Trace frist_trace = new Trace();
				frist_trace.setCompanyNum(companyNum);
				frist_trace.setDepartment(department);
				String regtime = dfs.format(new Date());
				frist_trace.setRegtime(regtime);
				
				order.setDepartment(department);
				
				allDAO.getTraceDAO().save(frist_trace);
				allDAO.getOrderFromDAO().attachDirty(order);
				redo_flag(companyNum, department);
				return companyNum+" 登记成功！！";
			}
			//已登记在本部门
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()==null&&trace_list.get(0).getDepartment().equals(department)){
				return companyNum+" 已登记，请不要重复扫描！！*";
			}
			//登记，但上一部门未离开
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()==null&&!(trace_list.get(0).getDepartment().equals(department))){
				String outtime = dfs.format(new Date());
				trace_list.get(0).setOuttime(outtime);
				
				Trace frist_trace = new Trace();
				frist_trace.setCompanyNum(companyNum);
				frist_trace.setDepartment(department);
				String regtime = dfs.format(new Date());
				frist_trace.setRegtime(regtime);
				
				allDAO.getTraceDAO().attachDirty(trace_list.get(0));
				allDAO.getTraceDAO().save(frist_trace);
				redo_flag(companyNum, department);
				return companyNum+" 登记成功，上一个部门离开时，未扫描，请提醒其注意！！*";
			}
			//已离开，重复扫描
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()!=null&&trace_list.get(0).getDepartment().equals(department)){
				return companyNum+" 已离开，请将订单交至下一部门！！*";
			}
			//登记，上一部门正常操作
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()!=null&&!(trace_list.get(0).getDepartment().equals(department))){
				Trace frist_trace = new Trace();
				frist_trace.setCompanyNum(companyNum);
				frist_trace.setDepartment(department);
				String regtime = dfs.format(new Date());
				frist_trace.setRegtime(regtime);
				
				order.setDepartment(department);
				
				allDAO.getTraceDAO().save(frist_trace);
				allDAO.getOrderFromDAO().attachDirty(order);
				redo_flag(companyNum, department);
				return companyNum+" 登记成功！！";
			}
		}
		
		if(status.equals("0")){
			//未登记就离开
			if(trace_list.size()==0||trace_list==null){
				return companyNum+" 未登记，不能离开！！*";
			}
			//正常操作离开
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()==null&&trace_list.get(0).getDepartment().equals(department)){
				String outtime = dfs.format(new Date());
				trace_list.get(0).setOuttime(outtime);
				allDAO.getTraceDAO().attachDirty(trace_list.get(0));
				return companyNum+" 离开,请将订单交至下一部门！！";
			}
			//上一部门未离开，且本部门未登记，直接离开
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()==null&&!(trace_list.get(0).getDepartment().equals(department))){
				return companyNum+" 已登记在"+department+"且未离开，本部门不能直接离开！！*";
			}
			//已离开，重复扫描
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()!=null&&trace_list.get(0).getDepartment().equals(department)){
				return companyNum+" 已离开，请不要重复扫描！！*";
			}
			//未登记直接离开
			if(trace_list.size()!=0&&trace_list!=null&&trace_list.get(0).getOuttime()!=null&&!(trace_list.get(0).getDepartment().equals(department))){
				return companyNum+" 未登记，不能直接离开！！*";
			}
		}
		return "有问题！！*";
	}
	
	
	@SuppressWarnings("unchecked")
	private void redo_flag(String companyNum, String department){
		List<Trace> trace_list = (List<Trace>) allDAO.getTraceDAO().findhql("from Trace t where t.companyNum = '"+companyNum+"' and t.department = '"+department+"'");
		List<Redo> redo_list = (List<Redo>) allDAO.getRedoDAO().findhql("from Redo r where r.companyNum = '"+companyNum+"' and r.department = '"+department+"'");
		if(trace_list.size()>1&&redo_list.size()==0){
			Redo redo = new Redo();
			redo.setCompanyNum(companyNum);
			redo.setDepartment(department);
			redo.setRedoNum("1");
			allDAO.getRedoDAO().save(redo);
		}
		if(trace_list.size()>1&&redo_list.size()!=0){
			String redoNum = redo_list.get(0).getRedoNum();
			redoNum = String.valueOf((Integer.valueOf(redoNum)+1));
			redo_list.get(0).setRedoNum(redoNum);
			allDAO.getRedoDAO().attachDirty(redo_list.get(0));
		}
	}

	public String test(@WebParam(name = "ttt")String ttt) {
		System.out.println(ttt);
		return "haha";
	}

}
